package fsoft.com.vn.directory.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import fsoft.com.vn.directory.form.Address;

@Repository
public class AddressDAOImpl implements AddressDAO {

	  @Autowired
      private SessionFactory sessionFactory;
      private Session session;

	@SuppressWarnings("unchecked")
	public List<Address> listAddress() {	
		return sessionFactory.getCurrentSession().createCriteria(Address.class).list();

	}


	public List<Address> search(String postcode, String street, String town) {
		session = sessionFactory.openSession();
        Criteria cr = session.createCriteria(Address.class);
        cr.add(Restrictions.like("postcode","%"+postcode+"%"));
        cr.add(Restrictions.like("address","%"+street+"%"));
        cr.add(Restrictions.like("town","%"+town+"%"));
        return cr.list();   

	}

}
